package com.community.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.pojo.Gather;
import com.community.pojo.Stock;
import com.community.service.IProductService;
import com.community.service.IStockDetailsService;
import com.community.service.IStockService;
import com.community.utils.PageUtils;
import com.community.vo.StockVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/community/stock")
public class StockController {

    @Autowired
    private IStockService stockService;

    @Autowired
    private IStockDetailsService stockDetailsService;

    @Autowired
    private IProductService productService;

    /**
     * 仓库表查询
     */
    @RequestMapping("/stockList")
    @ResponseBody
    public List<Stock> stockList() {
        List<Stock> list = stockService.list();
        return list;
    }

    /**
     * 查询所有仓库id
     * @return
     */
    @RequestMapping("/listStock")
    @ResponseBody
    public List<Stock> listStock() {
        List<Stock> list = stockService.listStock();
        System.out.println("list++++"+list);
        return list;
    }
    /**
     * 动态库存查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/show/{currentPage}/{pageSize}")
    public PageUtils<Stock> show(@PathVariable int currentPage,
                                 @PathVariable  int pageSize) {
//        QueryWrapper<Stock> qw = new QueryWrapper<Stock>();
//        qw.eq("CHECK_TAG", "S001-1");
        PageHelper.startPage(currentPage, pageSize);
        List<Stock> list = stockService.list();
        PageInfo<Stock> p = new PageInfo<Stock>(list);
        PageUtils<Stock> utils = new PageUtils<Stock>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    /**
     * 入库审核
     * @param vo
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/showtongguo/{currentPage}/{pageSize}")
    public PageUtils<Stock> showtongguo(@RequestBody StockVo vo, @PathVariable int currentPage, @PathVariable int pageSize) {
        QueryWrapper<Stock> qw = new QueryWrapper<Stock>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_stockId())) {
                qw.eq("stock_id", vo.getSearch_stockId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_stockName())) {
                qw.eq("stock_name", vo.getSearch_stockName());
            }
            if (!StringUtils.isEmpty(vo.getSearch_checkTime1())) {
                qw.ge("check_time", vo.getSearch_checkTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_checkTime2())) {
                qw.le("check_time", vo.getSearch_checkTime2());
            }
        }
        qw.eq("CHECK_TAG", "S001-1");

        PageHelper.startPage(currentPage, pageSize);
        List<Stock> list = stockService.list(qw);
        PageInfo<Stock> p = new PageInfo<Stock>(list);
        PageUtils<Stock> utils = new PageUtils<Stock>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    /**
     * 删除
     * @param stockId @returnID
     */
    @ResponseBody
    @RequestMapping("/del")
    public boolean del(int stockId) {
        boolean b = stockService.removeById(stockId);
        return b;
    }
    /**
     * 根据id查询所有入库的
     * @param stockId
     * @return
     */
    @RequestMapping("/showById")
    @ResponseBody
    public Stock showById(Integer stockId) {
        Stock byId = stockService.getById(stockId);
        return byId;
    }
    /**
     * 添加新仓库
     * @param stock
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Boolean add(@RequestBody Stock stock) {
        boolean save = stockService.save(stock);
        return save;
    }
    /**
     * 审核通过
     * @param stock
     * @return
     */
    @ResponseBody
    @RequestMapping("/update3/{currentPage}/{pageSize}")
    public boolean update3(@RequestBody Stock stock) {
        if (stock.getCheckTag() == null) {
            // 如果 checkTag 为 null，则设置为 "S001-1"
            stock.setCheckTag("S001-1");
        }
        boolean b = stockService.updateById(stock);
        return b;
    }
    /**
     * 修改仓单
     * @param stock
     * @return
     */
    @RequestMapping("/updateById")
    @ResponseBody
    public String updateById(@RequestBody Stock stock) {
        boolean b = stockService.updateById(stock);
        if(b){
            return "ok";
        }
        return "error";
    }
    /**
     * 查看仓库
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/view")
    public String view(int id) {
        Stock byId = stockService.getById(id);
        if (byId != null) {
            return "ok";
        }
        return "error";
    }
}
